package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.constant.UserConstant;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WeMediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname>
   implements ApUserRealnameService {

    @Autowired
    ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    ApUserMapper apUserMapper;

    @Autowired
    WeMediaFeign weMediaFeign;

    @Autowired
    ArticleFeign articleFeign;

    @Override
    public ResponseResult loadListByStatus(AuthDto authDto) {
        //调用mybatis-plus的api进行列表查询

        //1.参数校验
        if (authDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.分页参数校验
        authDto.checkParam();

        Page<ApUserRealname> page = new Page<>(authDto.getPage(), authDto.getSize());

        LambdaQueryWrapper<ApUserRealname> adApUserRealnameLambdaQueryWrapper = new LambdaQueryWrapper<>();


        if (authDto.getStatus() != null) {
            adApUserRealnameLambdaQueryWrapper.like(ApUserRealname::getStatus, authDto.getStatus());
        }

        IPage<ApUserRealname> adApUserRealnamePage = page(page, adApUserRealnameLambdaQueryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(authDto.getPage(), authDto.getSize(), (int) adApUserRealnamePage.getTotal());

        pageResponseResult.setData(adApUserRealnamePage.getRecords());

        return pageResponseResult;
    }

    @Override
    @GlobalTransactional
    @Transactional
    public ResponseResult authRealName(AuthDto authDto, Short status) {
        //1.参数校验
        if (authDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.正常更新
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(authDto.getId());
        apUserRealname.setStatus(status);
        if (StringUtils.isNotBlank(authDto.getMsg())){
            apUserRealname.setReason(authDto.getMsg());
        }
        boolean updateResult = this.updateById(apUserRealname);
        if (!updateResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAIL,"审核失败");

        }
        //3.做判断
       if (status== UserConstant.AUTH_PASS){
           //生成自媒体账号
           ApUserRealname userRealname = this.getById(authDto.getId());
           ApUser apUser = apUserMapper.selectById(userRealname.getUserId());
           //通过feign查询当前的App用户是否已经又自媒体账号，如果没有就进行创建
           WmUser wmUser = weMediaFeign.queryWmUserByName(apUser.getName());
           if (wmUser==null){
               //调用feign接口来新建自媒体账号
               wmUser = new WmUser();
               wmUser.setApUserId(apUser.getId());
               wmUser.setName(apUser.getName());
               wmUser.setPassword(apUser.getPassword());
               wmUser.setSalt(apUser.getSalt());
               wmUser.setPhone(apUser.getPhone());
               wmUser.setStatus(9);
               wmUser.setCreatedTime(new Date());
               weMediaFeign.saveWmUser(wmUser);
           }
           //更新apUser的flag为1
           apUser.setFlag((short)1);
           apUserMapper.updateById(apUser);
           //文章作者账号
           // 文章作者账号
           ApAuthor apAuthor = articleFeign.findByUserId(apUser.getId());
           if (null == apAuthor) {
               apAuthor = new ApAuthor();
               apAuthor.setName(apUser.getName());
               apAuthor.setType(2);
               apAuthor.setUserId(apUser.getId());
               apAuthor.setCreatedTime(new Date());
               articleFeign.save(apAuthor);
           }
           //通过feign接口来创建
       }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
